﻿#region Usings
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
#endregion

namespace BK.EF.ConsoleEF
{
    public class Category
    {
        #region Instance Properties
        public virtual ICollection<Product> Products { get; set; }
        public string CategoryId { get; set; }
        public string Name { get; set; }
        #endregion
    }

    public class Product
    {
        #region Instance Properties
        public virtual Category Category { get; set; }
        public string CategoryId { get; set; }
        public string Name { get; set; }
        public int ProductId { get; set; }
        #endregion
    }

    public class ProductContext : DbContext
    {
        public ProductContext()
        {
        }

        #region Instance Properties
        public ProductContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {
        }

        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
        #endregion
    }

    internal class Program
    {
        #region Class Methods
        private static void Main(string[] args)
        {
            using (var db = new ProductContext(ConfigurationManager.ConnectionStrings["BookKeeper"].ConnectionString))
            {
                // Add a food category 
                var food = new Category
                    {
                        CategoryId = "FOOD",
                        Name = "Foods"
                    };
                db.Categories.Add(food);
                int recordsAffected = db.SaveChanges();

                Console.WriteLine(
                    "Saved {0} entities to the database, press any key to exit.",
                    recordsAffected);

                Console.ReadKey();
            }
        }
        #endregion
    }
}